package com.transcendence.appwidget.twofive;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.transcendence.appwidget.twofour.TwoFourAppWidget;
import com.transcendence.core.utils.log.LogUtils;
import com.transcendence.appwidget.WidgetUpdateListener;

/**
 * @author joephone
 * @date 2023/10/28
 * @desc
 */
public class MyService extends Service {
//    private WidgetUpdateListener mListener;
//
//    private void notifyWidgetUpdate() {
//        if (mListener != null) {
//            try {
//                mListener.onUpdateWidget();
//            } catch (RemoteException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    // 实现WidgetUpdateListener接口
//    private final WidgetUpdateListener.Stub mBinder = new WidgetUpdateListener.Stub() {
//        @Override
//        public void onUpdateWidget() throws RemoteException {
//            // 接收到通知后，执行更新UI的操作
//            updateWidgetUI();
//        }
//    };
//
//    @Override
//    public IBinder onBind(Intent intent) {
//        return mBinder;
//    }
//
//    // 用户登录成功后，调用该方法通知小部件更新UI
//    private void onLoginSuccess() {
//        if (mListener != null) {
//            notifyWidgetUpdate();
//        } else {
//            bindWidgetProvider();
//        }
//    }
//
//    // 绑定小部件的Provider，以获取WidgetUpdateListener接口实例
//    private void bindWidgetProvider() {
//        Intent intent = new Intent(this, TwoFourAppWidget.class);
//        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_BIND);
//        int[] ids = AppWidgetManager.getInstance(this).getAppWidgetIds(new ComponentName(this, TwoFourAppWidget.class));
//        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
////         bindRemoteViewsService(intent, new ServiceConnection() {
////            @Override
////            public void onServiceConnected(ComponentName name, IBinder service) {
////                mListener = WidgetUpdateListener.Stub.asInterface(service);
////                notifyWidgetUpdate();
////            }
////
////            @Override
////            public void onServiceDisconnected(ComponentName name) {
////                mListener = null;
////            }
////        }, Context.BIND_AUTO_CREATE);
//    }
//
//    // 更新UI的操作
//    private void updateWidgetUI() {
//        // TODO: 更新小部件的UI
//        LogUtils.d("更新小部件的UI");
//    }

    public MyService(){
        LogUtils.d("MyService");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new MyBinder();
    }

    class MyBinder extends WidgetUpdateListener.Stub {
//        @Override
//        public String getStringFromService() throws RemoteException {
//            LogUtils.d( "service 端的方法被client调用了");
//            return "777888";
//        }

        @Override
        public void onUpdateWidget() throws RemoteException {
            // 接收到通知后，执行更新UI的操作
           LogUtils.d("接收到通知后，执行更新UI的操作");
        }
    }

    public void bindWidgetProvider() {

    }
}
